#ifndef USB_H
#define USB_H

//#include "main.h"
#include <pio/pio.h>
#include <pio/pio_it.h>
#include <usb/device/cdc-serial/CDCDSerialDriver.h>
#include <usb/device/cdc-serial/CDCDSerialDriverDescriptors.h>

/// Size in bytes of the buffer used for reading data from the USB & USART
#define DATABUFFERSIZE \
    BOARD_USB_ENDPOINTS_MAXPACKETSIZE(CDCDSerialDriverDescriptors_DATAIN)

/// Use for power management
#define USB_STATE_IDLE    0
/// The USB device is in suspend state
#define USB_STATE_SUSPEND 4
/// The USB device is in resume state
#define USB_STATE_RESUME  5

/// State of USB, for suspend and resume
extern unsigned char USBState;

/// VBus pin instance.
extern const Pin pinVbus;

/// Buffer for storing incoming USB data.
extern unsigned char usbBufferRead[DATABUFFERSIZE];
/// Buffer for storing outgoing USB data.
extern unsigned char usbBufferWrite[DATABUFFERSIZE];

extern char usbReadRunning;

//------------------------------------------------------------------------------
/// Handles interrupts coming from PIO controllers.
//------------------------------------------------------------------------------
extern void ISR_Vbus(const Pin *pPin);

//------------------------------------------------------------------------------
/// Configures the VBus pin to trigger an interrupt when the level on that pin
/// changes.
//------------------------------------------------------------------------------
extern void VBus_Configure( void );

//------------------------------------------------------------------------------
/// Callback invoked when data has been received on the USB.
//------------------------------------------------------------------------------
extern void UsbDataReceived(unsigned int unused,
                            unsigned char status,
                            unsigned int received,
                            unsigned int remaining);

//------------------------------------------------------------------------------
//         Callbacks re-implementation
//------------------------------------------------------------------------------
//------------------------------------------------------------------------------
/// Invoked when the USB device leaves the Suspended state. By default,
/// configures the LEDs.
//------------------------------------------------------------------------------
//extern void USBDCallbacks_Resumed(void);

//------------------------------------------------------------------------------
/// Invoked when the USB device gets suspended. By default, turns off all LEDs.
//------------------------------------------------------------------------------
//extern void USBDCallbacks_Suspended(void);

#endif
